package com.hotec.PROJECT_NAME.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysException;
import com.hotec.PROJECT_NAME.modules.sys.service.SysExceptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

@Slf4j
public class UtilsLog {
	private UtilsLog (){ }

	/**
	 * 记录日志:
	 * @param clazz 所在类名
	 * @param str 哪里异常
	 * @param e 什么异常
	 */
	public static void log(Class<?> clazz, String str, Exception e) {
		log.info(clazz.getSimpleName() + ": " + str + ", " + e.toString());

		handleSaveException(e);
	}

	/**
	 * 记录日志:
	 * @param clazz 所在类名
	 * @param e 什么异常
	 */
	public static void log(Class<?> clazz, Exception e) {
		log.info(clazz.getSimpleName() + ": " + e.toString());

		handleSaveException(e);
	}

	private static void handleSaveException(Exception e) {
		SysExceptionService sysExceptionService = UtilsSpring.getBean(SysExceptionService.class);
		Environment environment = UtilsSpring.getBean(Environment.class);

		sysExceptionService.save(
				new SysException()
						.setContent(ExceptionUtil.stacktraceToString(e))
						.setSystem(environment.getProperty("sys.type"))
		);
	}
}